


import 'package:zfth/pages/home/Home_index.dart';
import 'package:zfth/pages/user/User_index.dart';
import 'package:zfth/pages/user/User_login.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:convert' as convert;
import 'dart:core';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../../components/form/LFormPage.dart';
import '../../utils/SPutil.dart';
import '../../utils/Settings.dart';
import '../../utils/Utils.dart';
import '../../utils/WebSocketUtil.dart';
import '../../utils/eventbus/EventBus.dart';
import '../../utils/eventbus/EventBusMethod.dart';
import 'dart:io' show Directory, Platform;
import '../../utils/MyUpdateDialog.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../api/SysConfig.dart';
import '../utils/StoreData.dart';

class TabbarPage extends StatefulWidget{
  const TabbarPage({super.key});

  @override
  State<TabbarPage> createState() =>TabbarPageState();

}

class TabbarPageState extends State<TabbarPage> with AutomaticKeepAliveClientMixin{

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentIndex,keepPage: true);

  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;

  ///开始自定义业务
  int _flag = 0;
  late PageController _pageController;
  int _currentIndex = 0;

  final List<Widget> _pages = const[HomeIndexPage(),UserIndexPage()];
  final List<dynamic> tabars = [
    {"activeIcon":'shouye2.png',"unActiveIcon":'shouye.png','title':'首页'},
    {"activeIcon":'wode2.png',"unActiveIcon":'wode.png','title':'我的'}
  ];


  Widget createTabbar(){
    List? menuList = StoreData.menuList;
    if(menuList.isEmpty){
      EasyLoading.showError("当前登录人没有此软件的操作权限");
      return const UserLoginPage();
    }

    List<BottomNavigationBarItem> itemList = [];

    for(int i=0;i<tabars.length;i++ ){
      itemList.add(
        BottomNavigationBarItem(
          icon: _currentIndex==i?Image.asset("images/${tabars[i]['activeIcon']}",width: 22,height: 22,fit: BoxFit.cover,):Image.asset("images/${tabars[i]['unActiveIcon']}",width: 22,height: 22,fit: BoxFit.cover,),
          label: tabars[i]['title'],
        ),
      );
    }


    return BottomNavigationBar (
      currentIndex: _currentIndex,
      fixedColor: Settings.primaryColor,
      items: itemList,
      onTap: (index){
        setState(() {
          _currentIndex = index;
          _pageController.jumpToPage(index);
        });
      },

    );
  }

  ///自定义业务结束


  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: WillPopScope(
        onWillPop: () async{
          _flag ^= 1;
          Future.delayed(const Duration(seconds: 2), () => _flag = 0);
          EasyLoading.showToast("再按一次退出");
          return _flag == 0;
        },
        child:PageView(
          physics: const NeverScrollableScrollPhysics(),
          controller: _pageController,
          children: _pages,

        )
      ),
      bottomNavigationBar:createTabbar(),
    );
  }

}